package com.huanglj.himalaya.views;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.huanglj.himalaya.R;
import com.huanglj.himalaya.adapters.IndicatorAdapter;
import com.huanglj.himalaya.base.BaseApplication;
import com.huanglj.himalaya.fragments.RecommendFragment;
import com.huanglj.himalaya.interfaces.IRecommendCallBack;
import com.huanglj.himalaya.presenters.RecommendPresenter;

public abstract class UILoader extends FrameLayout {

    private View mLoadingView;
    private View mNetworkErrorView;
    private View mOnEmptyView;
    private View mSuccessView;
    private OnRetryClickListener mListener;
    public static SwipeRefreshLayout mRefreshLayout;

    public enum UIStatus{
        LOADING,SUCCESS,NETWORK_ERROR,EMPTY,NONE
    }
    public UIStatus mCurrentStatus = UIStatus.NONE;

    public UILoader(@NonNull Context context) {
        this(context,null);
    }

    public UILoader(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public UILoader(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //加载完内容后还得更新UI
        //init();
        //更新UI

    }

    public void updateStatus(UIStatus status){
        mCurrentStatus = status;
        BaseApplication.getHandler().post(new Runnable() {
            @Override
            public void run() {
                switchUIByCurrentStatus();
            }
        });
    }

    private void init() {
        switchUIByCurrentStatus();
    }

    private void switchUIByCurrentStatus() {
        //加载中
        if (mLoadingView == null) {
            mLoadingView = getLoadingView();
            addView(mLoadingView);
        }
        //根据状态设置成是否可见,如果是LOADING状态就可见
        mLoadingView.setVisibility(mCurrentStatus == UIStatus.LOADING ? VISIBLE : GONE);

        if (mNetworkErrorView == null) {
            mNetworkErrorView = getNetworkErrorView();
            addView(mNetworkErrorView);
        }
        //根据状态设置成是否可见,如果是NETWORK_ERRORG状态就可见
        mNetworkErrorView.setVisibility(mCurrentStatus == UIStatus.NETWORK_ERROR ? VISIBLE : GONE);

        if (mOnEmptyView == null) {
            mOnEmptyView = getOnEmptyView();
            addView(mOnEmptyView);
        }
        //根据状态设置成是否可见,如果是EMPTY状态就可见
        mOnEmptyView.setVisibility(mCurrentStatus == UIStatus.EMPTY ? VISIBLE : GONE);

        if (mSuccessView == null) {
            mSuccessView = getSuccessView();
            addView(mSuccessView);
        }
        //根据状态设置成是否可见,如果是EMPTY状态就可见
        mSuccessView.setVisibility(mCurrentStatus == UIStatus.SUCCESS ? VISIBLE : GONE);


    }

    protected abstract View getSuccessView();


    private View getOnEmptyView() {
        return LayoutInflater.from(getContext()).inflate(R.layout.fragment_empty,null,false);
    }

    private View getNetworkErrorView() {
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.fragment_network_error, null, false);
        mRefreshLayout = inflate.findViewById(R.id.refresh_item);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //去重新获取数据
                if (mListener != null) {
                    mListener.onRetryClick();
                    //如果获取不到数据就5秒后关闭
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (mRefreshLayout.isRefreshing()) {
                                mRefreshLayout.setRefreshing(false);
                            }
                        }
                    },5000);
                }
            }
        });
        return inflate;
    }

    private View getLoadingView() {
        return LayoutInflater.from(getContext()).inflate(R.layout.fragment_loading,null,false);
    }

    public void setOnRetryClickListener(OnRetryClickListener listener){
        mListener = listener;
    }

    public interface OnRetryClickListener{
        void onRetryClick();
    }
}
